/**
 * 
 */
package com.oolong.account.web;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.PostConstruct;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.oolong.account.model.InTypeModel;
import com.oolong.account.model.OutTypeModel;
import com.oolong.account.service.InManage;
import com.oolong.account.service.OutManage;

/**
 * @author rong.zhang
 * 
 */
// 标记它是个SPRING的BEAN，是MVC里的C
@Component
public class LoadGlobalParameter {
	Logger logger = Logger.getLogger(LoadGlobalParameter.class);
	// 表示需要SPRING自动注入
	@Autowired
	private OutManage outManage;
	@Autowired
	private InManage inManage;
	public static List<OutTypeModel> globaloutfathertypes;
	public static List<List<OutTypeModel>> globaloutsontypes;
	public static List<InTypeModel> globalinType;
	public static Map<Integer, String> sonType = new HashMap<Integer, String>();
	public static List<Integer> oneids = new ArrayList<Integer>();
	public static List<Integer> twoids = new ArrayList<Integer>();;

	@PostConstruct
	public void init() {
		initOutfatherData();
		initOutsonData();
		initInData();
		for (List<OutTypeModel> outTypeModels : globaloutsontypes) {
			for (OutTypeModel outTypeModel : outTypeModels) {
				sonType.put(outTypeModel.getId(), outTypeModel.getName());
			}
		}
	}

	public List<OutTypeModel> initOutfatherData() {
		// 加载支出父类型
		List<OutTypeModel> outfathertypes = outManage.querybygroupid(-1);
		globaloutfathertypes = outfathertypes;
		return globaloutfathertypes;
	}

	public List<List<OutTypeModel>> initOutsonData() {
		// 加载支出子类型
		List<List<OutTypeModel>> outsontypesList = new ArrayList();
		for (int i = 1; i <= globaloutfathertypes.size(); i++) {
			outsontypesList.add(outManage.querybygroupid(i));
			for (OutTypeModel outTypeModel : outManage.querybygroupid(i)) {
				twoids.add(outTypeModel.getId());
			}
		}
		globaloutsontypes = outsontypesList;
		return globaloutsontypes;
	}

	public List<InTypeModel> initInData() {
		// 加载收入类型
		globalinType = inManage.querybygroupid(-1);
		for (InTypeModel inTypeModel : inManage.querybygroupid(-1)) {
			oneids.add(inTypeModel.getId());
		}
		return globalinType;
	}
}
